import Vue from 'vue'
import Router from 'vue-router'
import {LoadingBar} from 'view-design'
import store from '@/store'
import {setToken, getToken, setTitle} from '@/utils'

Vue.use(Router)

const routers = new Router({
  mode: 'history',
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录页面'
      },
      component: () => import('@/views/login')
    }, {
      path: '/setting',
      name: 'setting',
      meta: {
        title: '数据管理',
        auth: true
      },
      component: resolve => require(['@/views/setting'], resolve),
      children:[{
        path: 'menu',
        name: 'settingMenu',
        meta: {
          title: '菜单管理',
          auth: true
        },
        component: resolve => require(['@/views/setting/sys/Menu'], resolve)
      }]
    }, {
      path: '/backend',
      name: 'backend',
      redirect: '/home',
      component: () => import('@/views/backend'),
      children: [
        {
          path: '/home',
          redirect: '/home/v1',
          meta: {
            title: '平台首页'
          },
          component: (resolve) => require(['@/views/home/vHome'], resolve),
          children: [
            {
              path: '/home/v1',
              component: (resolve) => require(['@/views/home/vips'], resolve)
            }, {
              path: '/home/list',
              component: (resolve) => require(['@/views/home/list'], resolve)
            }, {
              path: '/home/pays',
              component: (resolve) => require(['@/views/home/pays'], resolve)
            }
          ]
        }, {
          path: '/gateway',
          redirect: '/gateway/v1',
          meta: {
            title: '网关配置'
          },
          component: (resolve) => require(['@/views/gateway/vGateway'], resolve),
          children: [
            {
              path: '/gateway/v1',
              component: (resolve) => require(['@/views/gateway/vips'], resolve)
            }
          ]
        }, {
          path: '/apps',
          redirect: '/apps/v1',
          meta: {
            title: '应用中心'
          },
          component: (resolve) => require(['@/views/apps/vApps'], resolve),
          children: [
            {
              path: '/apps/v1',
              component: (resolve) => require(['@/views/apps/vips'], resolve)
            }, {
              path: '/apps/web',
              component: (resolve) => require(['@/views/apps/web'], resolve)
            }, {
              path: '/apps/python',
              component: (resolve) => require(['@/views/apps/python'], resolve)
            }
          ]
        }, {
          path: '/system',
          redirect: '/system/v1',
          meta: {
            title: '系统设置'
          },
          component: (resolve) => require(['@/views/system/vSystem'], resolve),
          children: [
            {
              path: '/system/v1',
              component: (resolve) => require(['@/views/system/vips'], resolve)
            }
          ]
        }
      ]
    }, {
      path: '/',
      name: 'index',
      meta: {
        title: '雨之和'
      },
      component: (resolve) => require(['@/views'], resolve),
      redirect: '/index',
      children: [
        {
          path: '/demo',
          name: 'demo',
          meta: {
            title: '项目实例'
          },
          component: (resolve) => require(['@/views/demo'], resolve)
        }, {
          path: '/about',
          name: 'about',
          meta: {
            title: '关于我'
          },
          component: (resolve) => require(['@/views/about'], resolve)
        }, {
          path: '/index',
          name: '首页',
          meta: {
            title: '首页内容'
          },
          component: (resolve) => require(['@/views/official'], resolve)
        }
      ]
    }, {
      path: '/ecosphere',
      name: 'ecosphere',
      meta: {
        auth: true,
        title: '生态圈'
      },
      component: (resolve) => require(['@/views/ecosphere'], resolve),
      redirect: '/ecosphere/leader',
      children: [
        {
          path: 'leader',
          name: 'leader',
          meta: {
            auth: true,
            title: '文档概要'
          },
          component: (resolve) => require(['@/views/ecosphere/Leader'], resolve)
        }, {
          path: 'document',
          name: 'document',
          meta: {
            auth: true,
            title: '文档说明'
          },
          component: (resolve) => require(['@/views/ecosphere/Document'], resolve)
        }
      ]
    }, {
      path: '/404',
      name: 'notFound',
      component: (resolve) => require(['@/views/exception'], resolve)
    }, {
      path: '*',
      redirect: '/404'
    }
  ]
})

routers.beforeEach(async (to, from, next) => {
  LoadingBar.start()
  /* const token = getToken()
  if (!token && to.name !== LOGIN_PAGE_NAME) {
    // 未登录且要跳转的页面不是登录页
    next({
      name: LOGIN_PAGE_NAME // 跳转到登录页
    })
  } else if (!token && to.name === LOGIN_PAGE_NAME) {
    // 未登陆且要跳转的页面是登录页
    next() // 跳转
  } else if (token && to.name === LOGIN_PAGE_NAME) {
    // 已登录且要跳转的页面是登录页
    next({
      name: homeName // 跳转到homeName页
    })
  } else {
    next()
  } */
  next()
})

routers.afterEach(to => {
  LoadingBar.finish()
})

export default routers
